package com.pickgliss.ui
{
	import com.greensock.easing.Sine;
	import com.pickgliss.loader.DisplayLoader;
	import com.pickgliss.utils.StringUtils;
	
	import flash.display.DisplayObjectContainer;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;

	public final class ComponentSetting
	{
		/**
		 * 按钮按住时开始 发出change的时间 
		 */		
		public static var BUTTON_PRESS_START_TIME:int = 500;
		/**
		 *  按钮按住时每次发出change的时间间隔 
		 */		
		public static var BUTTON_PRESS_STEP_TIME:int = 100;
		/**
		 * Combox执行隐藏缓动动画的缓动函数 
		 */		
		public static var COMBOBOX_HIDE_EASE_FUNCTION:Function = Sine.easeIn;
		/**
		 * Combox执行隐藏缓动动画的时间
		 */	
		public static var COMBOBOX_HIDE_TIME:Number = 0;
		/**
		 * Combox执行展示缓动动画的缓动函数 
		 */
		public static var COMBOBOX_SHOW_EASE_FUNCTION:Function = Sine.easeInOut;
		/**
		 * Combox执行展示缓动动画的时间
		 */
		public static var COMBOBOX_SHOW_TIME:Number = 0.5;
		/**
		 * bitmapData配置在XML中的标签名称 
		 */		
		public static var BITMAPDATA_TAG_NAME:String = "bitmapData";
		/**
		 * bitmap配置在XML中的标签名称 
		 */
		public static var BITMAP_TAG_NAME:String = "bitmap";
		/**
		 *  简单Alert的默认名称
		 */		
		public static var SIMPLE_ALERT_STYLE:String = "SimpleAlert";
		/**
		 * 功能提示框 
		 */		
		public static var FUNCTION_ALERT_STLE:String = "FunctionAlert";
		/**
		 * SimpleBitmapButton的filterString 
		 */		
		public static var SIMPLE_BITMAP_BUTTON_FILTER:String = "null,lightFilter,null,grayFilter";
		/**
		 *确定和取消按钮的间距 
		 */	
		public static var ALERT_BUTTON_GAPE:int = 30;
		
		public static var COMBOX_LIST_LAYER:DisplayObjectContainer;
		
		public static var SCROLL_UINT_INCREMENT:int = 1;
		public static var SCROLL_BLOCK_INCREMENT:int = 20;
		public static var DISPLACEMENT_OFFSET:uint = 1;
		public static var LANGUAGE:String;
		public static var RESOURCE_PATH : String="";
		public static var BACKUP_RESOURCE_PATH:String;
		
		public static var ResloveConfigXMLPathCall:Function = null;
		public static var ResloveConfigZipPathCall:Function = null;
		public static var ResloveConfigUISourcePathCall:Function = null;
		
		public static const ALPHA_LAYER_FILTER:String = "alphaLayerGilter";
		public static const CORE_MODULE_NAME:String = "core.xml";
		public static var SITE : String ;
		public static var MD5_OBJECT:Array = [];
		/**
		 * 所有资源文件的md5列表，有配置文件md5.xml得到 
		 */		
		public static var md5Dic:Dictionary = new Dictionary();
		/**
		 * 所有UI资源和代码文件的md5文件列表，这些资源都是需要进行md5验证，
		 * 并且在资源文件中包含md5信息 
		 */		
		public static var checkMD5:Dictionary = new Dictionary();
		/**
		 * 对应模块的配置文件地址 
		 * @param module
		 * @return 
		 * 
		 */		
		public static function getUIConfigXMLPath(module:String):String
		{
			var url : String;
			if(ResloveConfigXMLPathCall == null)
			{
				if(!DisplayLoader.isDebug)
				{
					url =  RESOURCE_PATH + "ui/"+LANGUAGE+"/xml/"+module+".xml";
				}
				return "ui/"+LANGUAGE+"/xml/"+module+".xml";
			}
			else
			{
				url =  ResloveConfigXMLPathCall(module);
			}
			trace("ComponentSetting.RESOURCE_PATH url : " + url);
			return url;
		}
		/**
		 * 对应模块的压缩配置文件地址 
		 * @return 
		 * 
		 */		
		public static function getUIConfigZIPPath():String
		{
			if(ResloveConfigZipPathCall == null)
			{
				if(!DisplayLoader.isDebug)
				{
					return RESOURCE_PATH + "ui/"+LANGUAGE+"/xml/xml.png";
				}
				return "ui/"+LANGUAGE+"/xml/xml.png";
			}else
			{
				return ResloveConfigZipPathCall();
			}
			
		}
		/**
		 * 对应模块的资源地址 
		 * @param module
		 * @return 
		 * 
		 */		
		public static function getUISourcePath(module:String):String
		{
			if(ResloveConfigUISourcePathCall == null)
			{
				if(!DisplayLoader.isDebug)
				{
					return RESOURCE_PATH + "ui/"+LANGUAGE+"/swf/"+module+".swf";
				}
				return "ui/"+LANGUAGE+"/swf/"+module+".swf";
			}else
			{
				return ResloveConfigUISourcePathCall(module);
			}
		}
		/**
		 * 对应模块的代码文件
		 * @param project
		 * 
		 */		
		public static function getProjectSourcePath(project:String):String
		{
			if(!DisplayLoader.isDebug)
			{
				return RESOURCE_PATH + project;
			}
			return project;
		}
		
		public static var storyMoviePathCall : Function;
		public static function getStoryMoviePath() : String
		{
			if(storyMoviePathCall != null)
			{
				return storyMoviePathCall();
			}
			return "";
		}
		
		/**
		 * 返回指定url加载的文件的md5信息 
		 * @param url
		 * @return 
		 * 
		 */		
		public static function getUrlMd5(url:String):String
		{
			var filePath:String = getMD5FilePathByUrl(url);
			if(!StringUtils.isEmpty(filePath))
			{
				var v : String =  md5Dic[filePath];
				if(v == null)v = "7road";
				return v;
			}
				
			else 
				return "";
		}
		
		public static function checkNeedAnalyzeMd5(md5:String):Boolean
		{
			return MD5_OBJECT.indexOf(md5) >= 0;
		}
		/**
		 * 从文件路劲中返回文件名：如从ui/zh_cn/swf/xx.swf中返回xx.swf ;
		 * @param path
		 * @return 
		 * 
		 */		
		public static function getMD5FileNameByFilePath(path:String):String
		{
			path = getMD5FilePathByUrl(path);
			var fileName:String;
			if(path.indexOf("/") >= 0)
			{
				fileName = path.substr(path.lastIndexOf("/") + 1,path.length);
			}
			else
			{
				fileName = path;
			}
			return fileName;
		}
		/**
		 * 去掉加载路径中？后面的参数
		 * @param path
		 * @return 
		 * 
		 */		
		private static function getMD5FilePathByUrl(path:String):String
		{
			if(path.indexOf('?') >= 0)
			{
				path = path.substring(0,path.indexOf('?'));
			}
			return path.toLowerCase();
		}
		/**
		 * 对比md5码是否匹配 
		 * @param temp
		 * @param fileName
		 * @return 
		 * 
		 */		
		public static function compareMD5(temp:ByteArray, md5:String):Boolean
		{
			var md5Bytes:ByteArray = new ByteArray();
			md5Bytes.writeUTFBytes(md5);
			
			md5Bytes.position = 0;
			temp.position = 5;
			while(md5Bytes.bytesAvailable > 0)
			{
				var source:int = md5Bytes.readByte();
				var target:int = temp.readByte();
				if(source != target)
				{
					trace("文件解码失败");
					return false;
				}
			}
			return true;
		}
	}
}